package dao

import (
	"xin-job/admin/dao/model"

	"gorm.io/gorm"
)

func LoadGroup(groupId int) *model.XxlJobGroup {
	var g model.XxlJobGroup
	db.First(&g, groupId)
	return &g
}

func FindByAddressType(addressType int) []model.XxlJobGroup {
	var groups []model.XxlJobGroup
	db.Where("address_type = ?", addressType).Order("app_name, title, id ASC").Find(&groups)
	return groups
}

func UpdateGroup(group *model.XxlJobGroup) {
	db.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(group).Updates(model.XxlJobGroup{
			AppName: group.AppName,
			Title: group.Title,
			AddressType: group.AddressType,
			AddressList: group.AddressList,
			UpdateTime: group.UpdateTime,
		}).Error; err != nil {
			return err
		}

		return nil
	})
}
